// Help
// 
// 保佑没Bug 
//
// Created by AS on 2019/4/8. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "LotteryTableViewCell.h"
#import "BarView.h"
#import "LhView.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface LotteryTableViewCell()
/**  */
@property (nonatomic,strong) UIImageView *iconImage;
/**  */
@property (nonatomic,strong) UILabel *nameLabel;
/**  */
@property (nonatomic,strong) UILabel *preiodLabel;
/**  */
@property (nonatomic,strong) UILabel *timeLabel;
/**  */
@property (nonatomic,strong) BarView *barView;
/**  */
@property (nonatomic,strong) LhView *lhView;

/**  */
@property (nonatomic,strong) MModel *model;
/**  */
@property (nonatomic,assign) Boolean hideIcon;

/**  */
@property (nonatomic,strong) UILabel *testLabel;
/**  */
@property (nonatomic,strong) NSTimer *timer;

/**  */
@property (nonatomic,strong) UIView *openNumberStatusView;


@end


@implementation LotteryTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubView];
    }
    return self;
}


-(void)initSubView{
    
    self.iconImage = [UIImageView new];
    self.iconImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.iconImage];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = Font16;
    self.nameLabel.textColor = HexRGB(0x222222);
    [self.contentView addSubview:self.nameLabel];
    
    self.preiodLabel = [UILabel new];
    self.preiodLabel.font = Font16;
    self.preiodLabel.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.preiodLabel];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.font = Font14;
    self.timeLabel.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.timeLabel];
    
    self.barView = [BarView new];
    [self.contentView addSubview:self.barView];
    
    self.lhView = [LhView new];
    [self.contentView addSubview:self.lhView];
    
    self.testLabel = [UILabel new];
    self.testLabel.font = Font14;
    self.testLabel.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:self.testLabel];
    
    self.openNumberStatusView = [UIView new];
    self.openNumberStatusView.backgroundColor = HexRGB(0xF5D94A);
    [self.openNumberStatusView setLayerWithRadius:15 color:ClearColor width:0.0];
    self.openNumberStatusView.hidden = YES;
    [self addSubview:self.openNumberStatusView];
    
    UILabel *textLabel = [UILabel new];
    textLabel.font = Font14;
    textLabel.text = @"正在开奖中...";
    textLabel.textColor = HexRGB(0x222222);
    [self.openNumberStatusView addSubview:textLabel];
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_offset(ZoomHeight(40));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImage.mas_right).offset(10);
        make.top.equalTo(self.contentView).offset(10);
    }];
    [self.preiodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(5);
        make.bottom.equalTo(self.nameLabel);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.preiodLabel.mas_right).offset(5);
        make.bottom.equalTo(self.nameLabel);
    }];
    
    [self.testLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.timeLabel);
        make.left.equalTo(self.timeLabel.mas_right).offset(5);
        
    }];
    
    [self.barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.bottom.equalTo(self.iconImage);
    }];
    
    [self.lhView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
        make.bottom.equalTo(self.iconImage);
    }];
    
    
    
    [self.openNumberStatusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.barView);
        make.left.equalTo(self.iconImage.mas_right).offset(10);
        make.right.equalTo(textLabel).offset(15);
    }];
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.openNumberStatusView).offset(15);
        make.top.equalTo(self.openNumberStatusView).offset(5);
        make.bottom.equalTo(self.openNumberStatusView.mas_bottom).offset(-5);
        make.right.equalTo(self.openNumberStatusView.mas_right).offset(-15);
    }];
    
    [self startTime];
}



-(void)startTime{
//    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countdown) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

-(void)countdown{
//    if (self.model) {
//        self.testLabel.text = [[NSDate date] dateFormat:@"mm:ss"];
//        NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
//
//        if (self.model.nextInterval <= currentTime) {
//            [self getNewOpenData];
//        }
//    }
}



-(void)getNewOpenData{
    self.openNumberStatusView.hidden = NO;
    self.lhView.hidden = YES;
    self.barView.hidden = YES;
    NSString *url = @"/api/lotteryOpenNumber/getNewestOpenNumberByLotteryCategory";
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    dict[@"lotteryCategoryCode"] = self.model.subCode;
    dict[@"number"] = @"1";
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        if (!error) {
            if (!msg) {
                if ([self.model.code isEqualToString:@"lhc"]) {
                    self.lhView.hidden = NO;
                    self.barView.hidden = YES;
                }else{
                    self.barView.hidden = NO;
                    self.lhView.hidden = YES;
                }
                NSLog(@"%@",[result mj_JSONString]);
            }
        }
    }];
    
}


-(void)setModel:(MModel *)model withHideIcon:(Boolean)hideIcon{
    self.model = model;
    self.hideIcon = hideIcon;
    self.nameLabel.text = model.name;
    
    self.timeLabel.text = model.openTime;

    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:model.iconUrl] placeholderImage:[UIImage createImageWithColor:HexRGB(0xeeeeee)]];
    
    if ([model.code isEqualToString:@"lhc"]) {
        self.lhView.hidden = NO;
        self.barView.hidden = YES;
        self.lhView.model = model;
    }else{
        self.barView.model = model;
        self.barView.hidden = NO;
        self.lhView.hidden = YES;
    }
    self.iconImage.hidden = hideIcon;
    self.nameLabel.hidden = hideIcon;
    if (hideIcon) {
        self.preiodLabel.text = [NSString stringWithFormat:@"第%@期",model.preiods];
        [self.preiodLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImage);
        }];
        [self.nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImage);
        }];
        [self.openNumberStatusView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImage);
        }];
    }else{
        NSString *preiods = [model.preiods substringFromIndex:model.preiods.length - 4];
        self.preiodLabel.text = [NSString stringWithFormat:@"第%@期",preiods];
    }
    
}

-(void)dealloc{
    NSLog(@"cell dealloc");
}

-(void)didMoveToSuperview{
//    UIViewController *controller = [self getViewController];
//    //这里需要判断相应的controller是否存在
//    if (controller){
//        @weakify(self)
//        [[controller rac_willDeallocSignal] subscribeCompleted:^{
//            @strongify(self)
//            [self stopTime];
//        }];
//    }
}


-(void)stopTime{
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

@end
